// 1、无头单向非循环链表实现
public class SingleLinkedList implements IList {
    static class ListNode {
        public int val;
        public ListNode next;

        //提供一个构造方法
        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;

    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        if (this.head == null){
            this.head = node;
        }else {
            node.next = this.head;
            this.head = node;
        }
    }

    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        ListNode cur = this.head;
        if (this.head == null){
            this.head = node;
        }else {
            while (cur.next != null){
                cur = cur.next;
            }
            cur.next = node;
        }
    }

    @Override
    public void addIndex(int index, int data) throws PosIllegality {
        if (index < 0 || index > size()){
            throw new PosIllegality("插入位置不合法！"+index);
        }
        if (index == 0){
            addFirst(data);
            return;
        }
        if (index == size()){
            addLast(data);
            return;
        }
        ListNode node = new ListNode(data);
        ListNode cur = searchPrev(index);
        node.next = cur.next;
        cur.next = node;
    }

    private ListNode searchPrev(int index) {
        ListNode cur = this.head;
        int count = 0;
        while (count != index-1){
            cur = cur.next;
            count++;
        }
        return cur;
    }

    @Override
    public boolean contains(int key) {
        ListNode cur = this.head;
        while (cur != null){
            if (cur.val == key){
                return true;
            }else {
                cur = cur.next;
            }
        }
        return false;
    }

    @Override
    public void remove(int key) {
        if (this.head == null){
            return;
        }
        if (this.head.val == key){
            this.head = this.head.next;
            return;
        }
        ListNode cur = findPrev(key);
        if (cur == null){
            System.out.println("没有你要删除的数字");
            return;
        }
        ListNode del = cur.next;
        cur.next = del.next;
    }

    private ListNode findPrev(int key) {
        ListNode cur = this.head;
        while (cur.next != null){
            if (cur.next.val == key){
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }

    @Override
    public void removeAllKey(int key) {
        if (this.head == null){
            return;
        }
        ListNode cur = this.head;
        ListNode prev = this.head.next;
        while (cur != null){
            if (cur.val == key){
                prev.next = cur.next;
                cur = cur.next;
            }else {
                prev = cur;
                cur = cur.next;
            }
        }
        if (this.head.val == key){
            this.head = this.head.next;
        }
    }

    @Override
    public int size() {
        int count = 0;
        ListNode cur = this.head;
        while (cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }

    @Override
    public void display() {
        ListNode cur = this.head;
        while (cur != null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    @Override
    public void clear() {
        ListNode cur = this.head;
        while (cur != null){
            ListNode curNext = this.head.next;
            cur.next = null;
            cur = curNext;
        }
        this.head = null;
    }
}
